<!--
 * @Description:
 * @Author:
 * @Date: 2020-05-15 14:58:56
 * @LastEditors  : liuyi
 * @LastEditTime : 2020-05-15 17:42:12
 -->
<template>
  <div
    id="processApproveComponents"
    v-loading="loading"
    class="main_page"
  >
    <el-card
      class="process-card"
      style="height: 70px"
    >
      <el-row :gutter="8">
        <el-col style="text-align: center;">
          <font class="processTitle">
            <font style="margin-left:10px;">
              {{ processData.name }}
            </font>
          </font>
        </el-col>
      </el-row>
    </el-card>
    <el-card
      class="process-card"
      style="margin-top: 1px;height: calc(100% - 142px); overflow-y: auto"
    >
      <el-tabs
        v-model="activeName"
        @tab-click="handleClick"
      >
        <el-tab-pane
          label="业务表单"
          name="form"
        >
          <div style="padding: 20px">
            <el-row>
              <generate-from
                v-if="showForm"
                ref="form"
                :json-data="jsonFormData"
                @getFormData="getFormData"
              />
            </el-row>
          </div>
        </el-tab-pane>
        <el-tab-pane
          label="业务流程"
          name="process"
          lazy
        >
          <detail
            :process-obj="processData"
            type="create"
          />
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <el-card
      style="margin-top: 1px;height: 70px"
      :body-style="{ 'text-align': 'center' }"
    >
      <el-button
        type="primary"
        style="margin-right:100px;bottom: 0px"
        @click="startTaskProcess"
      >
        提交流程
      </el-button>
      <el-button
        style="margin-left:100px"
        @click="returnCreateTask"
      >
        取消
      </el-button>
    </el-card>
  </div>
</template>
<script>
import generateFrom from '@/components/Form/ext/GenerateForm'
import detail from '../components/detail'
import moment from 'moment'
import { getStartFormKey, startProcess } from '@/api/bpmn/bpmn'
export default {
  components: { generateFrom,detail },
  data() {
    return {
      jsonFormData:{
        'list': [],
        'config': {
          'labelWidth': 100,
          'labelPosition': 'right',
          'tableName': '',
          'size': 'mini'
        }
      },
      processData:{},
      procDefId_:'',
      loading:false,
      showForm:false,
      currentUserId:JSON.parse(sessionStorage.getItem('user')).account,
      activeName:'form',
      user:JSON.parse(sessionStorage.getItem('user'))
    };
  },
  created() {
    if (this.$route.params.data){
      sessionStorage.setItem('currProcessData', JSON.stringify(this.$route.params.data))
    }
    this.processData = JSON.parse(sessionStorage.getItem('currProcessData'))
    this.procDefId_ = this.processData.bpmn_key,
    getStartFormKey(this.procDefId_).then(res =>{
      this.jsonFormData = JSON.parse(res.key)
      this.showForm = true
      console.log(' this.jsonFormData',  this.jsonFormData)
    })
  },
  mounted() {

  },
  methods: {
    PrefixInteger(num, n){
      return (Array(n).join(0) + num).slice(-n)
    },
    getQuestionNumber(){
      const time = moment().format('YYYY-MM-DD HH:mm:ss') + this.PrefixInteger(Math.floor(Math.random()*(1 - 100) + 100),4).repeat(/-/g,'').repeat(/:/g,'')
      const randoms = this.PrefixInteger(Math.floor(Math.random()*(1 - 100) + 100),4)
      const timeStr = time.replace(/-/g,'').replace(/:/g,'').replace(' ','') + randoms
      return timeStr
    },
    handleClick(){},
    startTaskProcess(){
      this.$refs.form.handleSubmit()
    },
    returnCreateTask(){
      this.$router.push({
        name: 'check_process'
      });
    },
    getFormData(data){
      console.log('getFormData:',data)
      this.loading = true
      const self = this
      data = JSON.parse(data)
      const form = {
        variables:{
          'questionNumber':{
            'value': this.getQuestionNumber()
          }
        },
        businessKey:'eps'
      }
      for(let prop in data){
        if ( data[prop] instanceof Array || data[prop] instanceof Object ){
          form.variables[prop] = {
            'value':JSON.stringify(data[prop])
          }
        } else{
          form.variables[prop] = {
            'value':data[prop]
          }
        }
      }
      const option = {
        key:this.processData.bpmn_key,
        form:form
      }
      startProcess(option).then(() =>{
        this.loading = false
        self.$message({
          message: `发送流程-${this.processData.name}-成功`,
          type: 'success',
          duration: 1500
        })
        this.$router.push({
          name: 'check_process'
        });
      }).catch(() =>{
        this.loading = false
        self.$message({
          message: `发送流程-${this.processData.name}-失败`,
          type: 'error',
          duration: 1500
        })
      })
    }
  }
};
</script>

<style lang="less" scoped>
  #processApproveComponents .processTitle {
    font-weight: bold;
    font-size: 20px;
    margin-left: 20px;
    margin-top: 20px;
  }

  #processApproveComponents #headDetail .ant-form-item {
    margin-bottom: 0;
  }

  .textareaview {
    word-wrap: break-word;
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
  }
  .required::before {
    display: inline-block;
    margin-right: 4px;
    content: '*';
    font-family: SimSun;
    line-height: 1;
    font-size: 14px;
    color: #f5222d;
  }
  .el-card__body {
    background-color:#ffffff !important;
    color: #666 !important;
  }
  .main_page{
    height: 100%;
  }
</style>
